package com.base.utils;

import lombok.Data;

import java.io.Serializable;

/**
 * 统一返回结果
 *
 * @author Chloe
 */
@Data
public class Result implements Serializable {

    private static final long serialVersionUID = -3948389268046368059L;

    private Integer code;

    private String message;

    private Boolean success = true;

    private Object result;

    private Long timestamp = System.currentTimeMillis();

    public Result() {
    }

    public Result(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    public static Result success() {
        Result result = new Result();
        result.setResultCode(ResultCode.SUCCESS);
        return result;
    }

    public static Result success(Object data) {
        Result result = new Result();
        result.setResultCode(ResultCode.SUCCESS);
        result.setResult(data);
        return result;
    }

    public static Result failure(String message) {
        Result result = new Result();
        result.setSuccess(false);
        result.setResultCode(ResultCode.INTERFACE_INNER_INVOKE_ERROR);
        result.setMessage(message);
        return result;
    }

    public static Result failure(String message, Object data) {
        Result result = new Result();
        result.setSuccess(false);
        result.setResultCode(ResultCode.INTERFACE_INNER_INVOKE_ERROR);
        result.setMessage(message);
        result.setResult(data);
        return result;
    }

    public static Result failure(ResultCode resultCode) {
        Result result = new Result();
        result.setSuccess(false);
        result.setResultCode(resultCode);
        return result;
    }

    public static Result failure(ResultCode resultCode, Object data) {
        Result result = new Result();
        result.setSuccess(false);
        result.setResultCode(resultCode);
        result.setResult(data);
        return result;
    }

    /**
     * 没有权限访问
     *
     * @return
     */
    public static Result noAuth() {
        return Result.failure(ResultCode.PERMISSION_NO_ACCESS);
    }

    public void setResultCode(ResultCode code) {
        this.code = code.code();
        this.message = code.message();
    }

}
